<?php
namespace Admin\Controller;
use Think\Controller;
class AdminController extends Controller {

    /**
     *  管理员信息
     */
    public function admin(){
        $admin_user = M('user');
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}

        if($admin['user_role'] ==5){
            $admin_list = $admin_user->where("user_role >1")
                ->field('user_id,user_name,user_phone,user_email,user_role,role_id,role_name')
                ->join('role on role.role_id = user.user_role')
                ->select();
        }else{
            $admin_list = $admin_user->where("user_role >1 and user_role<>5")
                ->field('user_id,user_name,user_phone,user_email,user_role,role_id,role_name')
                ->join('role on role.role_id = user.user_role')
                ->select();
        }
        $this->assign('data',$admin_list);
        $this->display();
    }

    //管理员添加
    public function admin_add(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $msg=I('');
        if(empty($msg)){
            $this->display();
        } else {
            $data['user_name'] = I('post.user_name')?I('post.user_name'):$this->ajaxReturn(['status'=>0,'msg'=>'请输入管理员名称']);
            $data['user_password'] = I('post.user_password')? md5(md5(I('post.user_password'))):$this->ajaxReturn(['status'=>0,'msg'=>'请输入管理员密码']);
            $data['user_email'] = I('post.user_email')?I('post.user_email'):$this->ajaxReturn(['status'=>0,'msg'=>'请输入管理员登录邮箱']);
            $data['user_phone'] = I('post.user_phone')?I('post.user_phone'):$this->ajaxReturn(['status'=>0,'msg'=>'请输入管理员手机号']);
            $data['user_role'] = I('post.user_role')?I('post.user_role'):$this->ajaxReturn(['status'=>0,'msg'=>'请选择管理员角色']);
            $data['country'] = I('post.country')?I('post.country'):'China';
            $data['group'] = I('post.group')?I('post.group'):'TEST';

            $user_email = M('user')->where(['user_email'=>$data['user_email']])->find();
            $user_phone = M('user')->where(['user_phone'=>$data['user_phone']])->find();

             if(!empty($user_email)){
                 $this->ajaxReturn(array('msg'=>"邮箱号重复",'status'=>0));
             }else if(!empty($user_phone)){
                 $this->ajaxReturn(array('msg'=>"手机号重复",'status'=>0));
             }else if(!empty($user_email) && !empty($user_phone)){
                 $this->ajaxReturn(array('msg'=>"邮箱和手机号重复",'status'=>0));
             }else{
                 $add = M('user')->add($data);
                 if($add){
                     $this->ajaxReturn(array('msg'=>"添加成功",'status'=>1));
                 }else{
                     $this->ajaxReturn(array('msg'=>"添加失败",'status'=>0));
                 }
             }

        }
    }
    //管理员修改
    public function admin_save(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $id = I('id');
        if(empty($id)){
            $this->error('非法操作');exit;
        }
        $data = M('user')->where(['user_id'=>$id])->join('role on role.role_id=user.user_role')->find();
        $this->assign('data',$data);
        $this->display();
    }
    //管理员修改
    public function admin_saveData(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $id = I('post.id');
        if(empty($id)){
            $this->ajaxReturn(array('msg'=>"非法操作",'status'=>0));exit;
        }
        $admin_data = M('user')->where(['user_id'=>$id])->find();
        $data['user_name'] = I('post.user_name')?I('post.user_name'):die($this->ajaxReturn(['status'=>0,'msg'=>'请输入管理员名称']));
        $data['user_email'] = I('post.user_email')?I('post.user_email'):die($this->ajaxReturn(['status'=>0,'msg'=>'请输入管理员登录邮箱']));
        $data['user_phone'] = I('post.user_phone')?I('post.user_phone'):die($this->ajaxReturn(['status'=>0,'msg'=>'请输入管理员手机号']));
        $data['user_role'] = I('post.user_role')?I('post.user_role'):die($this->ajaxReturn(['status'=>0,'msg'=>'请选择管理员角色']));
        $data['user_password'] = I('post.user_password')? md5(md5(I('post.user_password'))):$admin_data['user_password'];


        if($admin_data['user_email']!=$data['user_email']){
            $email = M('user')->where(['user_email'=>$data['user_email']])->find();
        }
        if($admin_data['user_phone']!=$data['user_phone']){
            $phone =  M('user')->where(['user_phone'=>$data['user_phone']])->find();
        }
        if(!empty($email) && !empty($phone))   {
            $this->ajaxReturn(array('msg' => "邮箱和手机号重复", 'status' => 0));
        }else if(!empty($phone)){
            $this->ajaxReturn(array('msg'=>"手机号重复",'status'=>0));
        }else if(!empty($email)) {
            $this->ajaxReturn(array('msg'=>"邮箱重复",'status'=>0));
        }else{
            $save = M('user')->where(['user_id'=>$id])->save($data);
            if($save!==false){
                $this->ajaxReturn(array('msg'=>"修改成功",'status'=>1));
            }else{
                $this->ajaxReturn(array('msg'=>"修改失败",'status'=>0));
            }
        }
    }


    //删除
    public function admin_delete(){
        $admin = session('admin');
        if(empty($admin)){ $this->ajaxReturn(array('msg'=>"身份信息过期",'status'=>0));exit;}
        $id = I('post.id')?I('post.id'):die( $this->ajaxReturn(array('msg'=>"非法操作",'status'=>0)));
        $de = M('user')->where(['user_id'=>$id])->delete();
        if($de){
            $this->ajaxReturn(array('msg'=>"删除成功",'status'=>1));
        }else{
            $this->ajaxReturn(array('msg'=>"删除失败",'status'=>0));
        }
    }


    /**
     * 日志管理
     */
    public function Log(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $fo = I('fo') ? strtotime(I('fo')) : "";
        $to = I('to') ? strtotime(I('to')) : "";
        $log_type = I('log_type') ? I('log_type') : "";
        $log_user = I('log_user') ? I('log_user') : "";
        $where = '';
        if(!empty($log_user)){
            $where['log_user'] =$log_user;
        }
        if(!empty($log_type)){
            $where['log_type'] =$log_type;
        }
        if(!empty($of) && !empty($to)){
            $where['log_time'] = array('between',$fo.','.$to);
        }
        if(empty($where)){
            $count = M('log')->count();
            $Page = new \Think\Page($count,16);
            $show =$Page->show();
            $data = M('log')->order('log_id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        }else{
            $count = M('log')->where($where)->count();
            $Page = new \Think\Page($count,16);
            $Page->parameter['log_user']=$log_user;
            $Page->parameter['fo']=date('Y-m-d',$fo);
            $Page->parameter['to']=date('Y-m-d',$to);
            $Page->parameter['log_type']=$log_type;
            $show =$Page->show();
            $data = M('log')->where($where)->order('log_id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        }
        foreach($data as $k=>$v){
            $data[$k]['log_time']=date('Y-m-d H:i:s',$v['log_time']);
           if($v['log_type']==1){
                $data[$k]['log_type']="账号信息操作";
            }else if($v['log_type']==2){
                $data[$k]['log_type']="MT4信息操作";
            }else if($v['log_type']==3){
                $data[$k]['log_type']="资金变更操作";
            }else if($v['log_type']==4){
                $data[$k]['log_type']="审核变更操作";
            }
        }
        $this->assign('fo',date('Y-m-d',$fo));
        $this->assign('to',date('Y-m-d',$to));
        $this->assign('log_type',$log_type);
        $this->assign('log_user',$log_user);
        $this->assign('page',$show);
        $this->assign('data',$data);
        $this->display();
    }

    public function log_del(){
        $id = I('id');
        if(empty($id)){
            $this->ajaxReturn(array('status'=>0,'error'=>404));exit;
        }else{
            $data = M('log')->where(['log_id'=>$id])->find();
            if(!empty($data)){
                $this->ajaxReturn(array('status'=>1,'msg'=>$data['log_content']));
            }else{
                $this->ajaxReturn(array('status'=>0,'error'=>404));
            }
        }
    }
    //下载数据
    public function log_download(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $fo = I('fo') ? strtotime(I('fo')) : "";
        $to = I('to') ? strtotime(I('to')) : "";
        $log_type = I('log_type') ? I('log_type') : "";
        $log_user = I('log_user') ? I('log_user') : "";

        $where = '';
        if(!empty($log_user)){
            $where['log_user'] =$log_user;
        }
        if(!empty($log_type)){
            $where['log_type'] =$log_type;
        }
        if(!empty($of) && !empty($to)){
            $where['log_time'] = array('between',$fo.','.$to);
        }

        if(empty($where)){
            echo "请查询后在下载";die;
        }else{
            $data = M('log')->where($where)->order('log_id desc')->select();
        }
        $csv = new \Think\Csv();
        foreach($data as $k=>$v){
            $data[$k]['log_time']=date('Y-m-d H:i:s',$v['log_time']);
            if($v['log_type']==1){
                $data[$k]['log_type']="账号信息操作";
            }else if($v['log_type']==2){
                $data[$k]['log_type']="MT4信息操作";
            }else if($v['log_type']==3){
                $data[$k]['log_type']="资金变更操作";
            }else if($v['log_type']==4){
                $data[$k]['log_type']="审核变更操作";
            }
        }

        $csv_title=array('ID','操作人','操作时间','操作类型','操作内容');
        $csv->put_csv($data, $csv_title, "日志信息");
    }

    public function registerSwitch(){//是否允许二元、外汇注册的开关
        $msg=M('register_switch')->field('id,binary,foreign')->find();
        $this->assign('msg',$msg);
        $this->display();
    }

    public function setRegisterSwitch(){//设置注册开关
        $id=I('post.id')?I('post.id'):die(json_encode(array('status'=>0,'msg'=>'error')));
        $type=I('post.type')?I('post.type'):die(json_encode(array('status'=>0,'msg'=>'非法操作')));
        $status=I('post.status')?I('post.status'):0;
        if($type==1){
            $data['binary']=$status;
        }else{
            $data['foreign']=$status;
        }
        $res=M('register_switch')->where(['id'=>$id])->save($data);
        if($res){
            $this->ajaxReturn(array('msg'=>"操作成功",'status'=>1));
        }else{
            $this->ajaxReturn(array('msg'=>"操作失败",'status'=>0));
        }
    }
}